package cn.fangline.camerademo02;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * Created by 抽离 on 2018/10/31.
 */
public class LineActivity extends Activity implements SensorEventListener {


	private PlaneView pv_bubble;

	private int MAX_ANGLE = 90;//具体多大，自己尝试下就行了

	private Sensor gyroSensor = null;

	private SensorManager sensorManager;

	private int width, height;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 全屏显示窗口
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_line);
		pv_bubble = findViewById(R.id.pv_bubble);

		WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels;         // 屏幕宽度（像素）
		height = dm.heightPixels;       // 屏幕高度（像素）


		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_GAME); //为传感器注册监听器

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (null != sensorManager || null != gyroSensor) {
			sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_GAME); //为传感器注册监听器
		}
	}


	@Override
	protected void onPause() {
		// 取消方向传感器的监听
		if (null != sensorManager) {
			sensorManager.unregisterListener(this);
		}
		super.onPause();
	}

	//保存上一次 x y z 的坐标
	private float mPosX = 200;
	private float mPosY = 0;

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (Sensor.TYPE_ACCELEROMETER != event.sensor.getType()) {
			return;
		}
		int X_lateral = (int) event.values[0];
		int Y_longitudinal = (int) event.values[1];
		int Z_vertical = (int) event.values[2];

		if (Z_vertical < 0) {
			Toast.makeText(this,"手机屏幕请勿朝下", Toast.LENGTH_LONG).show();
		}

		mPosX -= X_lateral * 2;
		mPosY += Y_longitudinal * 3;
		//检测小球是否超出边界
		int maxScreenW = pv_bubble.maxScreenW;
		int maxScreenH = pv_bubble.maxSreenH;
		if (mPosX < 0) {
			mPosX = 0;
		} else if (mPosX > maxScreenW) {
			mPosX = maxScreenW;
		}
		if (mPosY < 0) {
			mPosY = 0;
		} else if (mPosY > maxScreenH) {
			mPosY = maxScreenH;
		}
		pv_bubble.bubbleX = mPosX;
		pv_bubble.bubbleY = mPosY;
		pv_bubble.postInvalidate();
	}




	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

}
